package com.cskaoyan.service;


import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.mapper.MarketBrandMapper;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.wxhomeindexvo.*;
import com.cskaoyan.mapper.*;
//import org.apache.commons.beanutils.BeanUtils;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.BeanUtils;

import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import javax.annotation.Resource;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:TODO
 * @author: landz
 * @create: 2023-01-01 23:46
 * @version:
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {

    @Resource
    MarketAdMapper marketAdMapper;

    @Resource
    MarketBrandMapper marketBrandMapper;

    @Resource
    MarketCategoryMapper marketCategoryMapper;

    @Resource
    MarketCouponMapper marketCouponMapper;

    @Resource
    MarketGoodsMapper marketGoodsMapper;

    @Resource
    MarketTopicMapper marketTopicMapper;

    /**
     * 获取所有的banner信息
     *
     * @return java.util.List<com.cskaoyan.bean.vo.wxhomeindexvo.BannerVo>
     * @author landz
     * @create 2023/1/2 11:41
     **/
    @Override
    public List<MarketAd> getBanner() {
        MarketAdExample example = new MarketAdExample();
        MarketAdExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketAd> marketAds = marketAdMapper.selectByExample(example);
        return marketAds;
    }

    /**
     * 获取所有的brandList信息
     *
     * @return java.util.List<com.cskaoyan.bean.vo.wxhomeindexvo.BrandListVo>
     * @author landz
     * @create 2023/1/2 14:24
     **/
    @Override
    public List<WxHomeIndexbrandListVo> getBrandList() {
        //先查所有数据
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        //分页：分一页，每页4个信息
        PageHelper.startPage(1,4);

        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(example);

        //对同名数据赋值，返回
        List<WxHomeIndexbrandListVo> collect = marketBrands.stream().map(cate -> {
            WxHomeIndexbrandListVo wxHomeIndexbrandListVo = new WxHomeIndexbrandListVo();
            BeanUtils.copyProperties(cate, wxHomeIndexbrandListVo);
            return wxHomeIndexbrandListVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取所有的channel信息
     *
     * @return java.util.List<com.cskaoyan.bean.MarketCategory>
     * @author landz
     * @create 2023/1/2 17:38
     **/
    @Override
    public List<WxHomeIndexChannelVo> getChannel() {
        //先查所有数据
        MarketCategoryExample example = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);

        ///分页：分一页，每页4个信息
        PageHelper.startPage(1,4);

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);

        //对同名的属性赋值，返回

        List<WxHomeIndexChannelVo> collect = marketCategories.stream().map(cate -> {
            WxHomeIndexChannelVo wxHomeIndexChannelVo = new WxHomeIndexChannelVo();
            BeanUtils.copyProperties(cate, wxHomeIndexChannelVo);
            return wxHomeIndexChannelVo;
        }).collect(Collectors.toList());
        return collect;

    }

    /**
     * 获取所有的couponList信息
     * 
     * @return java.util.List<com.cskaoyan.bean.vo.wxhomeindexvo.CouponListVo> 
     * @author landz
     * @create 2023/1/2 19:50
     **/
    @Override
    public List<CouponListVo> getCouponList() {
        //先查数据库中所有的方法
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        ////分页：分一页，每页4个信息
        PageHelper.startPage(1,4);

        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(example);
        //对同名的属性数值，返回
        List<CouponListVo> collect = marketCoupons.stream().map(cate -> {
            CouponListVo couponListVo = new CouponListVo();
            BeanUtils.copyProperties(cate, couponListVo);
            return couponListVo;
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 获取所有的floorGoodsList
     * 
     * @return java.util.List<com.cskaoyan.bean.vo.wxhomeindexvo.FloorGoodsListVo> 
     * @author landz
     * @create 2023/1/2 20:04
     **/
    @Override
    public List<FloorGoodsListVo> getFloorGoodsList() {
        List<MarketCategory> goodsCategoryInfo = marketCategoryMapper.getGoodsCategoryInfo();

        ArrayList<FloorGoodsListVo> floorGoodsListVos = new ArrayList<>();

        for (MarketCategory marketCategory : goodsCategoryInfo) {

            //分页：分一页，每页4个信息
            PageHelper.startPage(1,4);

            List<GoodsListVo> goodsListVos = marketGoodsMapper.selectGoodsGroupByCategoryId(marketCategory.getId());
            if (!StringUtil.isEmpty(String.valueOf(goodsListVos))) {
                FloorGoodsListVo floorGoodsListVo = new FloorGoodsListVo();
                floorGoodsListVo.setGoodsLists(goodsListVos);
                floorGoodsListVo.setName(marketCategory.getName());
                floorGoodsListVo.setId(marketCategory.getId());
                floorGoodsListVos.add(floorGoodsListVo);
            }
        }
        return floorGoodsListVos;

//        FloorGoodsListVo floorGoodsListVo = new FloorGoodsListVo();
//
//        //查询数据库中所有数据
//        MarketGoodsExample example = new MarketGoodsExample();
//        MarketGoodsExample.Criteria criteria = example.createCriteria();
//        criteria.andDeletedEqualTo(false);
//        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);
//
//        //找出对应的同名类赋值，返回
//        List<GoodsListVo> collect = marketGoods.stream().map(cate -> {
//            GoodsListVo goodsListVo = new GoodsListVo();
//            BeanUtils.copyProperties(cate, goodsListVo);
//            return goodsListVo;
//        }).collect(Collectors.toList());
//
//        //查询所有外层的id和name数据
//        MarketCategoryExample example1 = new MarketCategoryExample();
//        MarketCategoryExample.Criteria criteria1 = example1.createCriteria();
//        criteria1.andDeletedEqualTo(false).andPidEqualTo(0);
//
//        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example1);
//
//        //找出对应的同名类赋值
//        List<FloorGoodsListVo> collect1 = marketCategories.stream().map(cate -> {
//            FloorGoodsListVo floorGoodsListVo1 = new FloorGoodsListVo();
//            BeanUtils.copyProperties(cate, floorGoodsListVo1);
//            return floorGoodsListVo1;
//        }).collect(Collectors.toList());
//
//        floorGoodsListVo.setGoodsLists(collect);
//        floorGoodsListVo.set


//        bigerrrrrrlist()
//        for (FloorGoodsListVo goodsListVo : collect1) {
//            floorGoodsListVo.setGoodsLists(new ArrayList<>());
//            List<GoodsListVo> goodsLists = floorGoodsListVo.getGoodsLists();
//            Integer id = goodsListVo.getId();
//            List<GoodList> goods = mapper.seler***();
//            goodsLists.add(goods);
//            floorGoodsListVo.setId();
//            floorGoodsListVo.setName();
//            bigerrrrrrlist.add(floorGoodsListVo);
//        }
//
//        //分装进入
//        collect1.stream()
    }

    /**
     * 获取所有的hotGoodsList
     *
     * @return java.util.List<com.cskaoyan.bean.vo.wxhomeindexvo.HotGoodsListVo>
     * @author landz
     * @create 2023/1/2 20:09
     **/
    @Override
    public List<HotGoodsListVo> getHotGoodsList() {
        //获取数据库中的所有数据
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        //分页：分一页，每页4个信息
        PageHelper.startPage(1,4);

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);

        //对同名的属性数值，返回
        List<HotGoodsListVo> collect = marketGoods.stream().map(cate -> {
            HotGoodsListVo hotGoodsListVo = new HotGoodsListVo();
            BeanUtils.copyProperties(cate, hotGoodsListVo);
            return hotGoodsListVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取所有的newGoodsList
     * 
     * @return java.util.List<com.cskaoyan.bean.vo.wxhomeindexvo.NewGoodsListVo> 
     * @author landz
     * @create 2023/1/2 21:18
     **/
    @Override
    public List<NewGoodsListVo> getNewGoodsList() {
        //从数据库中查出这张表的所有信息
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        //分页：分一页，每页4个信息
        PageHelper.startPage(1,4);

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);

        //对同名的属性赋值，返回
        List<NewGoodsListVo> collect = marketGoods.stream().map(cate -> {
            NewGoodsListVo newGoodsListVo = new NewGoodsListVo();
            BeanUtils.copyProperties(cate, newGoodsListVo);
            return newGoodsListVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取所有的topicList
     * 
     * @return java.util.List<com.cskaoyan.bean.vo.wxhomeindexvo.TopicListVo> 
     * @author landz
     * @create 2023/1/2 21:25
     **/
    @Override
    public List<TopicListVo> getTopicList() {
        //从数据库中找出所有对应的数据
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        //分页：分一页，每页4个信息
        PageHelper.startPage(1,4);
        
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);

        //找到前端需求的同名类，对应赋值
        List<TopicListVo> collect = marketTopics.stream().map(cate -> {
            TopicListVo topicListVo = new TopicListVo();
            BeanUtils.copyProperties(cate, topicListVo);
            return topicListVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
